import requests
import json
import re
import os
import sys

cwd = os.getcwd()
sys.path.append(cwd)
print(cwd)

from pub_func import DownloadFile, get_list_file, file_readlines, folder_exist

headers = {
    'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
}


if __name__ == '__main__':
    base_dir = '/media/ubuntu/08f29263-3882-43db-96fd-febb6f2e2e5f/cloud_type/huitu.com'
    list_file_name = get_list_file(base_dir, '.csv')
    for file_name in list_file_name:
        content = file_readlines(file_name)
        for img_info in content:
            img_name = img_info.split(',')[0]
            img_desc = img_info.split(',')[1]
            img_url = img_info.split(',')[2].strip()
            print(img_name, img_desc, img_url)
            
            dir_path = base_dir + '/' + img_name.split('_')[0]
            folder_exist(dir_path)
            DownloadFile(img_url, dir_path, img_name)
